package memleak.test.com.testlist;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.gzgd.log.LogUtils;


/**
 * Created by runrun on 2017/3/8.
 */

public class ChannelView extends RelativeLayout implements OnViewFun<LiveChannel>, View.OnClickListener {

    private static final String TAG = "ChannelView";
    private ImageView mLogo, mReview;
    private TextView mTitle, mNum;
    private LiveChannel mBean;

    private LiveApplication mApp;
    private PreferencesDataSource mPreferencesDataSource;
    private int mPosition;
    private OnRecyclerViewItemSelectedListener mOnRecyclerViewItemSelectedListener;
    private float mDefaultSize;

    public ChannelView(Context context) {
        this(context, null);
    }

    public ChannelView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChannelView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mApp = (LiveApplication) context.getApplicationContext();
    }

    public int getPosition() {
        return mPosition;
    }

    public void setPosition(int pos) {
        mPosition = pos;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mLogo = (ImageView) findViewById(R.id.logo);
        mReview = (ImageView) findViewById(R.id.review);
        mTitle = (TextView) findViewById(R.id.title);
        mNum = (TextView) findViewById(R.id.num);
        mDefaultSize = mNum.getTextSize();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setOnClickListener(this);
        setWillNotDraw(false);
        mPreferencesDataSource = PreferencesDataSource.newInstance();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (!changed || !isAttachedToWindow() || View.VISIBLE != getVisibility()) {
            return;
        }
        super.onLayout(changed, l, t, r, b);
    }

    public void setOnRecyclerViewItemSelectedListener(OnRecyclerViewItemSelectedListener listener) {
        mOnRecyclerViewItemSelectedListener = listener;
    }

    @Override
    public void updateView(LiveChannel channelBean) {
        if (null == channelBean) {
            return;
        }
        mBean = channelBean;
        String name = mBean.getcName();
        mTitle.setText(name + "");
        String number = mBean.getcNo();
        mNum.setText(number + "");
        if (!TextUtils.isEmpty(number) && number.length() > 3) {
            mNum.setTextSize(TypedValue.COMPLEX_UNIT_PX, mDefaultSize * 3 / number.length());
        } else {
            mNum.setTextSize(TypedValue.COMPLEX_UNIT_PX, mDefaultSize);
        }
        if (null != mBean.getCaps() && mBean.getCaps().contains("TSTV")) {
            mReview.setImageResource(R.mipmap.review);
        } else {
            mReview.setImageDrawable(null);
        }
        Glide.with(getContext().getApplicationContext()).load(channelBean.getcLogoUrl()).placeholder(R.mipmap.default_logo).into(mLogo);
    }

    public LiveChannel getLiveChannel() {
        return mBean;
    }

    @Override
    public void jump() {

        LogUtils.i(TAG, "playLive : " + mBean.getcName());

        String no = mBean.getcNo();
        String category = mApp.getCategoryId();
        //保存当前频道分类和播放频道号
        if (!TextUtils.isEmpty(no) && !TextUtils.isEmpty(category)) {
            mPreferencesDataSource.savePreChannelCategoryID(category);
            mPreferencesDataSource.savePreChannelNum(TransformUtils.parseInt(no, 999));
        }
        VideoBean bean = TransformBeanTool.transforVideoBean(mBean);
        //当前是否选中应用或者互动栏目去跳转
        if (mApp.adjustJump(bean)) {
            return;
        }
        if (null == bean) {
            return;
        }
        VideoBean curVideoBean = mApp.getCurVideoBean();
        if (null == curVideoBean) {
            mApp.adjustPlay(bean);
            mApp.changeChannelPos(mPosition);
        } else {
            //不重复播放逻辑
            if (null != bean.getVideoId() && !bean.getVideoId().equals(curVideoBean.getVideoId())) {
                mApp.adjustPlay(bean);
                mApp.changeChannelPos(mPosition);
            }
        }
       // StateManager.getInstance().fireEvent(State.EVENT_PRE_REFRESH_PF);
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

        if (gainFocus) {
            setBackgroundResource(R.mipmap.focus);
            if (null != mOnRecyclerViewItemSelectedListener) {
                mOnRecyclerViewItemSelectedListener.onItemSelected(mPosition);
            }
        } else {
            setBackground(null);
        }
        mTitle.setSelected(gainFocus);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isAttachedToWindow() || View.VISIBLE != getVisibility()) {
            return;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void draw(Canvas canvas) {
        if (!isAttachedToWindow() || View.VISIBLE != getVisibility()) {
            return;
        }
        super.draw(canvas);
    }

    @Override
    public void onClick(View v) {
        jump();
    }

}
